package com.wl.cloud.security.auto;

import com.wl.cloud.security.bean.IgnoreUrlsProperties;
import com.wl.cloud.security.bean.JwtProperties;
import com.wl.cloud.security.component.*;
import com.wl.cloud.security.register.LoginUserRegistry;
import com.wl.cloud.security.register.LoginUserRegistryImpl;
import com.wl.cloud.security.utils.JwtTokenUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @author: wanglin
 * @date: 2023-10-27 周五
 * @Version: 1.0
 * @Description: SpringSecurity自动配置
 */
@Configuration
@EnableConfigurationProperties({JwtProperties.class, IgnoreUrlsProperties.class})
public class SecurityAutoConfiguration {
    @ConditionalOnMissingBean(JwtAuthenticationTokenFilter.class)
    @Bean
    public JwtAuthenticationTokenFilter JwtAuthenticationTokenFilter() {
        return new JwtAuthenticationTokenFilter();
    }

    @ConditionalOnBean(JwtAuthenticationTokenFilter.class)
    @Bean
    public JwtTokenUtil jwtTokenUtil() {
        return new JwtTokenUtil();
    }

    @ConditionalOnMissingBean(JwtAccessDeniedHandler.class)
    @Bean
    public JwtAccessDeniedHandler jwtAccessDeniedHandler() {
        return new JwtAccessDeniedHandler();
    }

    @ConditionalOnMissingBean(JwtAuthenticationEntryPoint.class)
    @Bean
    public JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint() {
        return new JwtAuthenticationEntryPoint();
    }

    @ConditionalOnBean(name = "dynamicSecurityService")
    @Bean
    public DynamicAccessDecisionManager dynamicAccessDecisionManager() {
        return new DynamicAccessDecisionManager();
    }

    @ConditionalOnBean(name = "dynamicSecurityService")
    @Bean
    public DynamicSecurityMetadataSource dynamicSecurityMetadataSource() {
        return new DynamicSecurityMetadataSource();
    }

    @ConditionalOnBean(name = "dynamicSecurityService")
    @Bean
    public DynamicSecurityFilter dynamicSecurityFilter() {
        return new DynamicSecurityFilter();
    }

    @ConditionalOnBean(name = "dynamicSecurityService")
    @Bean
    public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
        return new InMemoryUserDetailsManager();
    }

    @ConditionalOnBean(JwtTokenUtil.class)
    @Bean
    public SessionRegistry getSessionRegistry() {
        return new SessionRegistryImpl();
    }

    @ConditionalOnBean(JwtTokenUtil.class)
    @Bean
    public LoginUserRegistry loginUserRegistry() {
        return new LoginUserRegistryImpl();
    }
}
